package com.xingmang.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingmang.common.constant.PageEntity;
import com.xingmang.common.constant.ResponseData;
import com.xingmang.common.constant.ResultBean;
import com.xingmang.common.dto.admin.ThirdCategoryInfoDTO;
import com.xingmang.common.dto.admin.ThirdCategorySaveDTO;
import com.xingmang.common.dto.admin.ThirdCategoryUpdateDTO;
import com.xingmang.common.dto.base.BasePageParam;
import com.xingmang.common.utils.TokenUtils;
import com.xingmang.common.vo.admin.ThirdCategoryVO;
import com.xingmang.db.entity.ThirdApp;
import com.xingmang.db.entity.ThirdCategory;
import com.xingmang.db.service.ThirdAppDBService;
import com.xingmang.db.service.ThirdCategoryDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: hansom
 * @Version: 1.0
 * @create 2023/6/19 10:49
 */
@Service
@Slf4j
public class ThirdCategoryService {

    @Resource
    private ThirdCategoryDBService thirdCategoryDBService;

    @Resource
    private ThirdAppDBService thirdAppDBService;


    public ResultBean<PageEntity<ThirdCategoryVO>> page(BasePageParam dto) {
        Page page = BeanUtil.copyProperties(dto, Page.class);
        Page<ThirdCategory> page1 = thirdCategoryDBService.page(page);
        PageEntity<ThirdCategoryVO> pageEntity = new PageEntity<>();
        BeanUtil.copyProperties(page1,pageEntity);
        List<ThirdCategoryVO> collect = page1.getRecords().stream().map(thirdCategory -> {
            ThirdCategoryVO vo = BeanUtil.copyProperties(thirdCategory, ThirdCategoryVO.class);
            vo.setCategoryId(thirdCategory.getId());
            return vo;
        }).collect(Collectors.toList());
        pageEntity.setRecords(collect);
        return ResponseData.success(pageEntity);
    }

    public ResultBean save(ThirdCategorySaveDTO dto) {
        Long userId = TokenUtils.getUserId();
        List<ThirdCategory> all = thirdCategoryDBService.list();
        if (all.size()>=5){
            return ResponseData.error("分类不能超过5个");
        }
//        Long userId = 1L;
        List<ThirdCategory> list = thirdCategoryDBService.list(new LambdaQueryWrapper<ThirdCategory>().eq(ThirdCategory::getCategoryName, dto.getCategoryName()));
        if (CollectionUtil.isNotEmpty(list)){
            return ResponseData.error("此分类名称已存在");
        }
        ThirdCategory thirdCategory = BeanUtil.copyProperties(dto, ThirdCategory.class);
        thirdCategory.setUserId(userId);
        thirdCategoryDBService.save(thirdCategory);
        return ResponseData.success(null);
    }

    public ResultBean update(ThirdCategoryUpdateDTO dto) {
        ThirdCategory thirdCategory = new ThirdCategory();
        thirdCategory.setId(dto.getCategoryId());
        thirdCategory.setCategoryName(dto.getCategoryName());
        thirdCategoryDBService.updateById(thirdCategory);
        return ResponseData.success(null);
    }

    public ResultBean delete(ThirdCategoryInfoDTO dto) {
        long count = thirdAppDBService.count(new LambdaQueryWrapper<ThirdApp>().eq(ThirdApp::getCategoryId, dto.getCategoryId()));
        if (count>0){
            return ResponseData.error("此分类下有应用，不能删除");
        }
        thirdCategoryDBService.removeById(dto.getCategoryId());
        return ResponseData.success(null);
    }

    public ResultBean<List<ThirdCategoryVO>> list() {
        List<ThirdCategory> list = thirdCategoryDBService.list();
        List<ThirdCategoryVO> thirdCategoryVOS = list.stream().map(thirdCategory -> {
            ThirdCategoryVO vo = BeanUtil.copyProperties(thirdCategory, ThirdCategoryVO.class);
            vo.setCategoryId(thirdCategory.getId());
            return vo;
        }).collect(Collectors.toList());
        return ResponseData.success(thirdCategoryVOS);
    }
}
